package cn.concurrence.cas;

import java.lang.reflect.Field;

import sun.misc.Unsafe;

/**
 * @author summit
 * @since 2022/4/20 11:24
 */
public class UnsafeTest {
	private static Unsafe unsafe;

	/**
	 * 无法直接使用，jav对其有安全检查，使用时会抛出异常，我这边通过反射的方式来获取unsafe
	 */
	static {
		Field theUnsafeInstance;
		try {
			theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
			theUnsafeInstance.setAccessible(true);
			unsafe = (Unsafe) theUnsafeInstance.get(Unsafe.class);
		} catch (NoSuchFieldException | IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	public  Integer value = 11110;

	public static void main(String[] args) throws NoSuchFieldException {
		//a = 1;
		System.out.println("------------------" + unsafe.objectFieldOffset(UnsafeTest.class.getDeclaredField("value")));
	}
}
